<template>
  <div :class="['switch', 'switch-' + size]" @click="doSwitch()">
    <div class="switch-cover" ref="cover"></div>
    <div class="switch-handler" ref="handler">
      <div class="switch-handler-circle"></div>
    </div>
  </div>
</template>
<style lang="scss">
  @import "../sass/var";
  @import "../sass/mixin";
  .switch {
    width: 96px;
    background-color: #fff;
    height: 52px;
    border-radius: 52px;
    position: relative;
    align-items: center;
    overflow: hidden;
    @include border(1px, solid, #ddd);
    background-color: $brand-primary;
    &-handler {
      width: 96px;
      height: 50px;
      border-radius: 50px;
      position: absolute;
      top: 0;
      left: 0;
      padding-right: 2px;
      background-color: #ffffff;
      transform: translateX(-46px);
      flex-direction: row;
      align-items: center;
      justify-content: flex-end;
      &-circle {
        width: 46px;
        height: 46px;
        border-radius: 46px;
        background-color: #ffffff;
        @include border(1, solid, #ddd);
        box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.38);
      }
    }
    &-cover {
      width: 100px;
      height: 50px;
      border-radius: 50px;
      transform: scale(1.2);
      background-color: #f2f2f2;
    }
  }
</style>
<script>
let animation = weex.requireModule('animation')
let theme = require('../include/theme')
module.exports = {
  model: {
    prop: 'value'
  },
  props: {
    value: {default: false}
  },

  data () {
    return {

    }
  },
  components: {},
  created () {
  },
  methods: {
    doSwitch () {
      this.value = !this.value
      let scale = this.value ? 0 : 1.2
      let translateX = this.value ? -2 : -46
      animation.transition(this.$refs.cover, {
        styles: {
          transform: 'scale(' + scale + ')'
        },
        duration: 500, // ms
        timingFunction: 'ease',
        needLayout: false,
        delay: 0 // ms
      }, function () {
      })
      animation.transition(this.$refs.handler, {
        styles: {
          transform: 'translateX(' + translateX + ')',
          backgroundColor: this.value ? theme.brand.primary : '#fff'
        },
        duration: 500, // ms
        timingFunction: 'ease',
        needLayout: false,
        delay: 0 // ms
      }, function () {
      })
      this.$emit('input', this.value)
      this.$emit('change', this.value)
    }
  }
}
</script>
